package com.tj.jinzao.presenter;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.tj.jinzao.api.SubscriberCallBack;
import com.tj.jinzao.base.BasePresenter;
import com.tj.jinzao.base.IBaseView;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.common.Settings;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.Health;
import com.tj.jinzao.model.HealthReport;
import com.tj.jinzao.model.ResultResponse;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 识别处理
 * Created by wjy 2018/12/10.
 */
public class HealthReportPresenter extends BasePresenter<IBaseView<List<HealthReport>>> {

    public static final String FACE_COLOR[] = new String[]{
            "面白", "面黑", "面红", "面黄", "面青", "正常"
    };

    public static final String FACE_GLOSS[] = new String[]{
            "有光泽", "少光泽", "无光泽"
    };

    public static final String LIP_COLOR[] = new String[]{
            "唇淡白", "唇淡红", "唇红", "唇暗红", "唇紫"
    };

    public static final String TONGUE_NATURE_COLOR[] = new String[]{
            "舌暗红", "舌淡白", "舌淡红", "舌红", "舌深红（舌紫）"
    };

    public static final String TONGUE_FAT_THIN[] = new String[]{
            "正常", "胖舌"
    };

    public static final String TONGUE_COAT_THICKNESS[] = new String[]{
            "薄苔", "厚苔"
    };

    public static final String TONGUE_CRACK[] = new String[]{
            "无裂纹", "有裂纹"
    };

    public static final String TONGUE_COAT_COLOR[] = new String[]{
            "苔白", "苔黄"
    };

    public HealthReportPresenter(IBaseView<List<HealthReport>> view) {
        super(view);
    }
    private String base64Image;
    public void getReportList() {
    }
    public void getReportDetail(long reportId) {
        addSubscription(mApiService.getHealthReport(reportId), new SubscriberCallBack<Health>(mView.getContext()) {

            @Override
            protected void onSuccess(Health report) {
                if (mView != null) {
                    List<HealthReport> list = new ArrayList();
                    String reportJson = report.getReportJson();
                    try {
                        JSONObject object = new JSONObject(reportJson);
                        HealthReport report1 = new Gson().fromJson(object.toString(),HealthReport.class);
                        list.add(report1);
                        mView.onRequestSuccess(list);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
//                    list.add(report.getReportJson());
                }
            }

            @Override
            protected void onFailure(ResultResponse response) {
            }
        });
    }

    public void addReport(String reportJson) {
        long id = Settings.getLong(mView.getContext(), Settings.KEY_PREF_FAMILY_ID, 0);
        HashMap<String, Object> map = new HashMap();
        if (id != 0) {
            map.put("familyId", id);
        }
        map.put("uid", UserManager.getInstance().getUser().getId());
        map.put("reportJson", reportJson);
        Log.i("数据接收结果http",""+UserManager.getInstance().getUser().getId()+"==="+reportJson);
        addSubscription(mApiService.addHealthReport(map), new SubscriberCallBack<HealthReport>(mView.getContext()) {

            @Override
            protected void onSuccess(HealthReport report) {
                if (mView != null) {
                    List<HealthReport> list = new ArrayList();
                    list.add(report);
                    mView.onRequestSuccess(list);
                }
            }

            @Override
            protected void onFailure(ResultResponse response) {


            }
        });
    }

    private final static String JSON = "application/json; charset=utf-8";

    /**
     * 智能诊断
     */
    public void examination() {

        String json = Settings.getString(mView.getContext(), Settings.KEY_PREF_ANSWER, "");
        if (TextUtils.isEmpty(json)) {
            return;
        }
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(MediaType.parse(JSON), json);
        Request request = new Request.Builder()
                .url(Constant.BASE_SERVER_URL + "api/examinations")
                .post(body)
                .addHeader("Authorization", String.valueOf(UserManager.getInstance().getUser().getAuthentication()))
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if (mView != null) {
                    mView.onRequestError(null);
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    String json = response.body().string();
                    Log.w("数据接收结果","qqq"+json);
//                    HealthReport report = new Gson().fromJson(json, HealthReport.class);
                    addReport(json);
//                    if (report != null && report.getId() != 0) {
//                        if (mView != null) {
//                            List<HealthReport> list = new ArrayList();
//                            list.add(report);
//                            mView.onRequestSuccess(list);
//                        }
//                    } else {
//                        if (mView != null) {
//                            mView.onRequestError(null);
//                        }
//                    }

                } catch (Exception e) {
                }
            }
        });
    }

}
